package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.CashBookDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CashBookTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CashBookServiceInt;

/**
 * Session Bean implementation class CashBookService
 */
@Stateless
@Remote({ CashBookServiceInt.class })
public class CashBookService implements CashBookServiceInt {

	@EJB
	private CashBookDAOInt cashBookDao;

	/**
	 * Default constructor.
	 */
	public CashBookService() {
	}

	@Override
	public void createCashBookEntry(CashBookTBL entry) {
		this.cashBookDao.persist(entry);

	}

	@Override
	public void removeCashBookEntry(Long id) {
		this.cashBookDao.delete(id);

	}

	@Override
	public CashBookTBL[] retrieveEntriesByYearAndMonth(int year, int month) {
		Object[] tmp = this.cashBookDao.retrieveCashBookEntryByYearAndMonth(
				year, month).toArray();

		CashBookTBL[] entries = new CashBookTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			entries[i] = (CashBookTBL) tmp[i];
		}
		return entries;

	}

	@Override
	public void updateCashBookItem(CashBookTBL cashBookTBL) {
		CashBookTBL tmpEntry = null;

		tmpEntry = this.cashBookDao.retrieveCashBookEntryByID(cashBookTBL
				.getId());

		tmpEntry.setDatum(cashBookTBL.getDatum());
		tmpEntry.setDay(cashBookTBL.getDay());
		tmpEntry.setEvent(cashBookTBL.getEvent());
		tmpEntry.setRevenue(cashBookTBL.getRevenue());
		tmpEntry.setInventory(cashBookTBL.getInventory());
		tmpEntry.setSpending(cashBookTBL.getSpending());

		this.cashBookDao.update(tmpEntry);
		System.out.println("End of update in service");
	}

	@Override
	public CashBookTBL[] retrieveCashEntriesByYearAndMonth(int year, int month,
			boolean cashEntries) {

		List<CashBookTBL> entryList = cashBookDao
				.retrieveCashBookEntryByYearAndMonth(year, month, cashEntries);

		Object[] tmp = null;

		if (entryList != null) {
			tmp = entryList.toArray();

			CashBookTBL[] entries = new CashBookTBL[tmp.length];

			for (int i = 0; i < tmp.length; i++) {
				entries[i] = (CashBookTBL) tmp[i];
			}
			return entries;
		}

		// Object[] tmp = this.cashBookDao.retrieveCashBookEntryByYearAndMonth(
		// year, month, cashEntries).toArray();

		return null;

	}

}
